/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author PhucAnh
 */
public class FillHeart extends Shape{
    Point pointStart;
    Point pointFinish;
    
    int xa,ya,xb,yb,xc,yc,xd,yd,xm,ym,xn,yn,tempx, tempy;
   
    int listX[] = new int[5];
    int listY[] = new int[5];
    
    public FillHeart() {

    }

    public FillHeart(Point pointStart, Point pointFinish, Color color, int basicStrok) {
        super(color, basicStrok);
        this.pointStart = pointStart;
        this.pointFinish = pointFinish;
        
        tempx = (pointFinish.getX() - pointStart.getX())/10;
        tempy = (pointFinish.getY() - pointStart.getY())/9;
        
        xm = pointStart.getX() + 4*tempx;
        ym = pointStart.getY();
        
        xn = pointStart.getX();
        yn = pointStart.getY();
        
        xa = (pointStart.getX() + pointFinish.getX())/2;
        ya = pointStart.getY() + 3*tempy;
        
        xb = pointFinish.getX() - tempx;
        yb = pointStart.getY() + 5*tempy;
        
        xc = xa;
        yc = pointFinish.getY();
        
        xd = (int) (pointStart.getX() + tempx/1.3);
        yd = yb;
        
        int width = pointFinish.getX() - pointStart.getX();
        int height = pointFinish.getY() - pointStart.getY();
        
        listX[0]=xa;
        listX[1]=xb;
        listX[2]=xc;
        listX[3]=xd;
        listX[4]=xa;
        
        listY[0]=ya;
        listY[1]=yb;
        listY[2]=yc;
        listY[3]=yd;
        listY[4]=ya;
        
}
    
        public void setPoint2(Point pointFinish){
            this.pointFinish = pointFinish;
        
        tempx = (pointFinish.getX() - pointStart.getX())/10;
        tempy = (pointFinish.getY() - pointStart.getY())/9;
        
        xm = pointStart.getX() + 4*tempx;
        ym = pointStart.getY();
        
        xn = pointStart.getX();
        yn = pointStart.getY();
        
        xa = (pointStart.getX() + pointFinish.getX())/2;
        ya = pointStart.getY() + 3*tempy;
        
        xb = pointFinish.getX() - tempx;
        yb = pointStart.getY() + 5*tempy;
        
        xc = xa;
        yc = pointFinish.getY();
        
        xd = (int) (pointStart.getX() + tempx/1.3);
        yd = yb;
        
        listX[0]=xa;
        listX[1]=xb;
        listX[2]=xc;
        listX[3]=xd;
        listX[4]=xa;
        
        listY[0]=ya;
        listY[1]=yb;
        listY[2]=yc;
        listY[3]=yd;
        listY[4]=ya;
        
}
    
    @Override
    public void drawShape(Graphics g,BufferedImage img)
    {
        Graphics2D g2= (Graphics2D)g;
                
        Point pointA = new Point(xa, ya);
        Point pointB = new Point(xb, yb);
        Point pointC = new Point(xc, yc);
        Point pointD = new Point(xd, yd);
        
        int width = pointFinish.getX() - pointStart.getX();
        int height = pointFinish.getY() - pointStart.getY();
        
        if(width >=0 && height >=0) {           
        Ellipse2D circle1 = new Ellipse2D.Double(xn, yn, 6*tempx, 6*tempy);
        Ellipse2D circle2 = new Ellipse2D.Double(xm, ym, 6*tempx, 6*tempy);
        
        Polygon polygon = new Polygon(listX, listY, 5);
        
        Area area1 = new Area(circle1);
	Area area2 = new Area(circle2);
        
	Area HeartShape = new Area(area2);
	HeartShape.add(area1);
		
	Area area3 = new Area(polygon);
	HeartShape.add(area3);

        g2.setStroke(new BasicStroke(this.getBasicStroke()));
        g2.setColor(this.getColor());
        g2.fill(HeartShape);
        }
    }
    @Override
    public void toMove(int m,int n)
    {
//        for(int i=0;i<5;i++){
//            listX[i]+=m;
//            listY[i]+=n;
//        }
//        xm+=m;
//        ym+=n;
//        xn+=m;
//        yn+=n;
//        
//        pointFinish.toMove(m, n);
//        pointStart.toMove(m, n);
        pointStart.toMove(m, n);
        pointFinish.toMove(m, n);
        setPoint2(pointFinish);
    }
    
    public boolean isContain(Point p){
        if(pointFinish.getX()>=pointStart.getX() && pointFinish.getY()>=pointStart.getY()){
            if(p.getX()>= pointStart.getX() && p.getX()<= pointFinish.getX() 
            && p.getY()>= pointStart.getY() && p.getY()<= pointFinish.getY()){
                return true;
            } else {
                return false;
            }
        } else if (pointFinish.getX()<pointStart.getX() && pointFinish.getY()<pointStart.getY()){
            if(p.getX()>= pointFinish.getX() && p.getX()<= pointStart.getX() 
            && p.getY()>= pointFinish.getY() && p.getY()<= pointStart.getY()){
                return true;
            } else {
                return false;
            }
        } else if (pointFinish.getX()>=pointStart.getX() && pointFinish.getY()<=pointStart.getY()){
            if(p.getX()>= pointStart.getX() && p.getX()<= pointFinish.getX() 
            && p.getY()>= pointFinish.getY() && p.getY()<= pointStart.getY()){
                return true;
            } else {
                return false;
            }
        } else {
            if(p.getX()>= pointFinish.getX() && p.getX()<= pointStart.getX() 
            && p.getY()>= pointStart.getY() && p.getY()<= pointFinish.getY()){
                return true;
            } else {
                return false;
            }
        }
    }
    
    public void replay(Graphics g,int time,Image a,Control.NewClass.MyPanel1 a2){
        int x=pointFinish.getX()- pointStart.getX();
        int y=pointFinish.getY()- pointStart.getY();
        Graphics2D g2D= (Graphics2D)g;
        g2D.setStroke(new BasicStroke(this.getBasicStroke()));
        g.setColor(this.getColor());
        Image img = a;
        Point tmp=new Point();
        for(int i=0;i<=time;i++){
          g.drawImage(img,0, 0, a2);
          tmp.setX(x*i/time+pointStart.getX());
          tmp.setY(y*i/time+pointStart.getY());
          setPoint2(tmp);
          int width = pointFinish.getX() - pointStart.getX();
          int height = pointFinish.getY() - pointStart.getY();
        
          if(width >=0 && height >=0) {           
          Ellipse2D circle1 = new Ellipse2D.Double(xn, yn, 6*tempx, 6*tempy);
          Ellipse2D circle2 = new Ellipse2D.Double(xm, ym, 6*tempx, 6*tempy);
        
          Polygon polygon = new Polygon(listX, listY, 5);
        
          Area area1 = new Area(circle1);
          Area area2 = new Area(circle2);
        
          Area HeartShape = new Area(area2);
          HeartShape.add(area1);
		
          Area area3 = new Area(polygon);
          HeartShape.add(area3);

          g2D.setStroke(new BasicStroke(this.getBasicStroke()));
          g.setColor(this.getColor());
          g2D.fill(HeartShape);
          }
          try {
                Thread.sleep(time);
              } catch (InterruptedException ex) {
          Logger.getLogger(Curve.class.getName()).log(Level.SEVERE, null, ex);
              }
        }
    }
}
